﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Security;
using MusicOnLine.Models;

namespace MusicOnLine.Common
{
    public class MOLRolesProvider:RoleProvider
    {
        private MusicPlayEntities DB = new MusicPlayEntities();
      
        public void MOLMembershipProvider()
        {
            
        }

        public override string ApplicationName
        {
            get
            {
                throw new NotImplementedException();
            }
            set
            {
                throw new NotImplementedException();
            }
        }

        public override void AddUsersToRoles(string[] usernames, string[] roleNames)
        {
            var userList = DB.Memberships.Where(o => usernames.ToList().IndexOf(o.UserName) >= 0).Select(o=>o.UserID).ToList();
            foreach (var item in userList)
            {
                var roleList = DB.Roles.Where(o => roleNames.ToList().IndexOf(o.RoleName) >= 0).Select(o => o.RoleID).ToList();
                foreach (var rItem in roleList)
                {
                    DB.UsersInRoles.Add(new UsersInRole() { UserID = item, RoleID = rItem });
                }
            }
            DB.SaveChanges();
        }

        public override void CreateRole(string roleName)
        {
            DB.Roles.Add(new Role() { RoleID = new Guid(), RoleName = roleName, Description = "" });
            DB.SaveChanges();
        }

        public override bool DeleteRole(string roleName, bool throwOnPopulatedRole)
        {
          var count=  DB.Roles.Join(DB.UsersInRoles, o => o.RoleID, o => o.RoleID, (x, y) =>new {roleID=x.RoleID,userID=y.UserID} ).Count();
          if (count > 0)
          {
              throw new Exception("角色中含有一个或多个成员");
          }
          else
          {
              var role = DB.Roles.Single(o => o.RoleName == roleName);
              DB.Roles.Remove(role);
              DB.SaveChanges();
              return true;
          }
        }

        public override string[] FindUsersInRole(string roleName, string usernameToMatch)
        {
             return   DB.Memberships.Where(o => o.UserName.Contains(usernameToMatch)).Select(o => new { userID = o.UserID, userName = o.UserName })
                .Join(DB.UsersInRoles, o => o.userID, o => o.UserID, (x, y) => new { roleID = y.RoleID,userName=x.userName }).Join(DB.Roles, o => o.roleID, o => o.RoleID, (x, y) => new { roleNmae=y.RoleName,x.userName})
                .Where(o=>o.roleNmae==roleName).Select(o=>o.userName).ToArray();
        }

        public override string[] GetAllRoles()
        {
            return DB.Roles.Select(o => o.RoleName).ToArray();
        }

        public override string[] GetRolesForUser(string username)
        {
            return DB.Memberships.Where(o => o.UserName==username).Select(o => new { userID = o.UserID})
                .Join(DB.UsersInRoles, o => o.userID, o => o.UserID, (x, y) => new { roleID = y.RoleID}).Join(DB.Roles, o => o.roleID, o => o.RoleID, (x, y) =>  y.RoleName)
                .ToArray();
        }

        public override string[] GetUsersInRole(string roleName)
        {
            return DB.Roles.Where(o => o.RoleName == roleName).Join(DB.UsersInRoles, o => o.RoleID, o => o.RoleID, (x, y) => new { userID = y.UserID })
                .Join(DB.Memberships, o => o.userID, o => o.UserID, (x, y) => y.UserName).ToArray();
        }

        public override bool IsUserInRole(string username, string roleName)
        {  
            var count = (from a in DB.Memberships
                         join b in DB.UsersInRoles on a.UserID equals b.UserID
                         join c in DB.Roles on b.RoleID equals c.RoleID
                         where a.UserName == username && c.RoleName == roleName
                         select a.UserID).Count();
            if (count > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
          
            
        }

        public override void RemoveUsersFromRoles(string[] usernames, string[] roleNames)
        {
            throw new NotImplementedException();
        }

        public override bool RoleExists(string roleName)
        {
            var count = DB.Roles.Where(o => o.RoleName == roleName).Count();
            if (count > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
    }
}